package org.jeecg.modules.qyxx.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.util.Rest;
import org.jeecg.modules.qyxx.entity.CompanyInfo;
import org.jeecg.modules.qyxx.entity.req.KfptDoServiceRequest;
import org.jeecg.modules.qyxx.entity.req.KfptSwyAppBaseRequest;
import org.jeecg.modules.qyxx.entity.req.NsrsbhShxydmRequst;
import org.jeecg.modules.qyxx.entity.resp.BaseResponse;
import org.jeecg.modules.qyxx.enumdemo.BszsResultEnum;
import org.jeecg.modules.qyxx.enumdemo.RedisKeyConstant;
import org.jeecg.modules.qyxx.service.IGetSessionService;
import org.jeecg.modules.qyxx.service.IUserCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class BszsCommonUtils {

    @Autowired
    private IUserCenterService iUserCenterService;

    @Autowired
    private IGetSessionService getSessionService;


    public String getSwjgdmService(String nsrsbh) {
        KfptDoServiceRequest kfptDoServiceRequest = new KfptDoServiceRequest();
        kfptDoServiceRequest.setNsrsbh(nsrsbh);
        Rest<BaseResponse> responseRest = iUserCenterService.getCompanyInfoByNsrsbh(kfptDoServiceRequest);
        if (responseRest == null || responseRest.getData() == null) {
            return "000000";
        }
        if (!"0".equals(responseRest.getData().getCode())) {
            return "000000";
        }
        CompanyInfo companyInfo = (CompanyInfo) responseRest.getData().getData();
        if (StringUtils.isBlank(companyInfo.getSwjgDm())) {
            return "000000";
        }
        return companyInfo.getSwjgDm();
    }

    public boolean setKfptDoServiceRequestFromRedis(KfptSwyAppBaseRequest req, String nsrsbh) throws Exception {
        Object sessionId = RedisUtil.get(RedisKeyConstant.redisKfptSessionKey + nsrsbh); //redis里取sessionId
        Object areaCode = RedisUtil.get(RedisKeyConstant.redisKfptAreaCodeKey + nsrsbh); //redis里取areacode
        Object gdslxDm = RedisUtil.get(RedisKeyConstant.redisKfptGdslxdmKey + nsrsbh); //redis里取gdslxdm
        Object clientNo = RedisUtil.get(RedisKeyConstant.redisKfptClientNoKey + nsrsbh); //redis里取clientno
        Object swjgdm = RedisUtil.get(RedisKeyConstant.redisKfptSwjgdmKey + nsrsbh);//redis里swjgdm
        if (sessionId != null) {
            req.setSessionId(sessionId.toString());
        } else {
            NsrsbhShxydmRequst nsrsbhShxydmRequst = new NsrsbhShxydmRequst();
            nsrsbhShxydmRequst.setShxydm(nsrsbh);
            nsrsbhShxydmRequst.setShxydm(nsrsbh);
            getSessionService.LoginByNsrsbh(nsrsbh);
            sessionId = RedisUtil.get(RedisKeyConstant.redisKfptSessionKey + nsrsbh); //redis里取sessionId
            areaCode = RedisUtil.get(RedisKeyConstant.redisKfptAreaCodeKey + nsrsbh); //redis里取areacode
            gdslxDm = RedisUtil.get(RedisKeyConstant.redisKfptGdslxdmKey + nsrsbh); //redis里取gdslxdm
            clientNo = RedisUtil.get(RedisKeyConstant.redisKfptClientNoKey + nsrsbh); //redis里取clientno
            swjgdm = RedisUtil.get(RedisKeyConstant.redisKfptSwjgdmKey + nsrsbh);//redis里swjgdm
        }
        if (areaCode != null) {
            req.setAreaCode(areaCode.toString());
        }
        if (gdslxDm != null) {
            req.setGdslxDm(gdslxDm.toString());
        }
        if (clientNo != null) {
            req.setClientNo(clientNo.toString());
        }
        if (swjgdm != null) {
            req.setSwjgDm(swjgdm.toString());
        } else {
            req.setSwjgDm(getSwjgdmService(nsrsbh));
            RedisUtil.set(RedisKeyConstant.redisKfptSwjgdmKey + nsrsbh, req.getSwjgDm(), 20 * 60L);
        }
        if ("36".equals(areaCode)) {
            if (sessionId == null) {
                log.error("设置值方法，纳税人识别号:{},sessionId:{},areaCode:{},gdslxDm:{},clientNo:{},swjgdm:{}", nsrsbh, sessionId, areaCode, gdslxDm, clientNo, swjgdm);
                return false;
            } else {
                return true;
            }
        }
        if (sessionId == null || areaCode == null || gdslxDm == null || clientNo == null) {
            log.error("设置值方法，纳税人识别号:{},sessionId:{},areaCode:{},gdslxDm:{},clientNo:{},swjgdm:{}", nsrsbh, sessionId, areaCode, gdslxDm, clientNo, swjgdm);
            return false;
        }
        return true;
    }

    public static Rest<BaseResponse> transBaseResponseToRest(BaseResponse response) {
        response.setMap(null);
        if (BszsResultEnum.SUCCESS_OLD.getCode().equals(response.getCode())) {
            response.setCode(BszsResultEnum.SUCCESS_NEW.getCode());
        }
        return new Rest(response);
    }

}